//
//  MyPmViewController.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/4/20.
//  Copyright © 2016年 game. All rights reserved.
//

#import "MyPmViewController.h"
#import "MyPmCell.h"
#import "MyPmModel.h"
#import "SpacePagerModel.h"
#import "SayHilloViewController.h"
#import "OtherInformationCtrl.h"
#import "RootTableview.h"
#import "MJRefresh.h"
@interface MyPmViewController ()<UITableViewDataSource,UITableViewDelegate,MyPmCellDeleget>

@property (nonatomic,strong) RootTableview *tableview;
@property (nonatomic,strong) NSMutableArray *dataAry;
@property (nonatomic,strong) MyPmModel *model;
@property (nonatomic,assign) CGFloat cellHight;
@property (nonatomic,assign) NSInteger nextpage;
@property (nonatomic,assign) BOOL IsRefresh;
@property (nonatomic,strong) NSMutableDictionary *cellHightDic;

@end

@implementation MyPmViewController

-(NSMutableDictionary *)cellHightDic{
    if (!_cellHightDic) {
        _cellHightDic = [NSMutableDictionary dictionary];
    }
    return _cellHightDic;
}


-(NSMutableArray *)dataAry{
    if (!_dataAry) {
        _dataAry = [NSMutableArray array];
    }
    return _dataAry;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.app.myTabBar.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    StatusBlack;
}
- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableview = [[RootTableview alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    
    [self.tableview registerNib:[UINib nibWithNibName:@"MyPmCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableview];
    self.tableview.separatorStyle = 0;
//    self.tableview.backgroundColor = tabPlainBackColor;
    
        
    self.tableview.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(RefreshFooter)];

    self.nextpage = 2;
    
    [self AchiveDataWhithPage:1];
    __weak typeof(self) weakself = self;
    _tableview.TapBlok = ^{ [weakself AchiveDataWhithPage:1]; };

}
-(void)RefreshFooter{
    
    SpacePagerModel *pageModel ;
    if (self.nextpage >pageModel.total_page.integerValue) {
        [self.tableview.footer noticeNoMoreData];
    }else
        [self AchiveDataWhithPage:self.nextpage];
    self.nextpage ++;
}

-(void)AchiveDataWhithPage:(NSInteger)page{
    
    
    [[Manager sharedManager]RequestWhithURL:[NSString stringWithFormat:Mypm,(long)page] AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        
        if (error) {
            self.tableview.isShowFile = YES;
            [self.tableview reloadData];
            [_tableview.footer endRefreshing];
        }else{
        self.model = [[MyPmModel alloc]initWithDictionary:responseObject[@"data"] error:nil];
        
        [self.dataAry addObjectsFromArray:self.model.list];
        
        self.tableview.isShowImage = YES;
        [self.tableview reloadData];
        
        [_tableview.footer endRefreshing];
        
        }
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataAry.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 1+[[self.cellHightDic valueForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]]floatValue];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    MyPmCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = self.dataAry[indexPath.row];
    cell.deleget = self;
     [self.cellHightDic setValue:[NSString stringWithFormat:@"%f",cell.size.height] forKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyPmCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    SayHilloViewController *hillo = [SayHilloViewController new];
    hillo.otherId = cell.Touid;
    [self.navigationController pushViewController:hillo animated:YES];
}

-(void)ToSpaceWhithUid:(NSString *)uid{
    OtherInformationCtrl *space = [OtherInformationCtrl new];
    space.uid = uid;
    [self.navigationController pushViewController:space animated:YES];
}


@end
